#coding=utf-8

import Queue
import threading
import requests
import re

class trd(threading.Thread):
    def __init__(self,que):
        threading.Thread.__init__(self)
        self.que=que

    def run(self):
        while not self.que.empty():
            url=self.que.get()
            self.spider(url)
    def spider(self,url):
        r=requests.get(url)
        courses=re.findall(r'<h3 class="course-card-name">(.*?)</h3>', r.content)
        for i in courses:
            print i

def main():
    url="https://www.imooc.com/course/list?page="
    pagenum=35
    threads=[]
    thread_count=17
    que=Queue.Queue()
    for i in range(1,pagenum):
        que.put(url+str(i))
    for i in range(0,thread_count):
        threads.append(trd(que))
    for i in range(0,thread_count):
        threads[i].start()

if __name__=="__main__":
    main()